<?php
// +----------------------------------------------------------------------
// | yian [ 壹乘加工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 张依然 <439583514@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 广告管理模块信息文件
// +------
namespace app\ads\controller;

use app\common\controller\Adminbase;
use app\ads\model\Ads as Ads_Model;
use app\ads\model\AdsPosition as AdsPosition_Model;
use think\Db;

/**
 * 广告管理
 */
class Adsposition extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
        $this->AdsPosition_Model = new AdsPosition_Model;
        $this->ads_Model = new Ads_Model;
    }

    /**
     * 广告管理列表
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 1);
            $where=['siteid'=>$this->siteid];
            $_list =  $this->AdsPosition_Model->where($where)->order('listorder', 'desc')->page($page, $limit)->select();
            foreach ($_list as $k=>$v){

                $_list[$k]['inputtime']=date('Y-m-d',$v['inputtime']);
                $_list[$k]['updatetime']=date('Y-m-d',$v['updatetime']);

            }
            $total = count($_list);
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }

        return $this->fetch();
    }

    /**
     * 新增广告管理
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证器
            $rule = [
                'title|广告位名称' => 'require',
                'name|广告位英文名称' => 'require',

            ];
            $title= $this->AdsPosition_Model->where(['title'=>$data['title']])->find();
            $name= $this->AdsPosition_Model->where(['name'=>$data['name']])->find();
            if($title){
                $this->error("广告位名称已经存在！");
            }
            if( $name){
                $this->error("广告位英文名称已经存在！");
            }
            $validate = new \think\Validate($rule);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $data['inputtime']=time();
            $data['updatetime']=time();
            $data['admin_id']=$this->admin_id;
            $data['siteid']=$this->siteid;
            $status = $this->AdsPosition_Model->allowField(true)->save($data);
            if ($status) {
                $this->success("添加成功！", url("ads/ads/index"));
            } else {
                $this->error("添加失败！");
            }
        } else {
            /*        $Terms = Db::name('Terms')->where(["module" => "links"])->select();
                    $this->assign("Terms", $Terms);*/
            return $this->fetch();
        }
    }

    /**
     * 编辑广告管理
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            //验证器
            $rule = [
                'title' => 'require',
                'name' => 'require',

            ];
            $msg = [
                'title.require' => '广告位名称',
                'name.require' => '广告位英文名称',

            ];
            $data['updatetime']=time();
            $data['admin_id']=$this->admin_id;
            $data['siteid']=$this->siteid;
            $title= $this->AdsPosition_Model->where(['title'=>$data['title']])->find();
            $name= $this->AdsPosition_Model->where(['name'=>$data['name']])->find();
            if($title['ad_id']!=$data['ad_id']){
                if($title){
                    $this->error("广告名称已经存在！");
                }
            }

            if($name['ad_id']!=$data['ad_id']){
                if( $name){
                    $this->error("广告英文名称已经存在！");
                }
            }
            $validate = new \think\Validate($rule, $msg);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $status = $this->AdsPosition_Model->allowField(true)->save($data, ['position_id' => $data['position_id']]);
            if ($status) {
                $this->success("编辑成功！", url("ads/Adsposition/index"));
            } else {
                $this->error("编辑失败！");
            }

        } else {
            $id = $this->request->param('id', 0);
            $data = $this->AdsPosition_Model->where(array("position_id" => $id))->find();
            if (!$data) {
                $this->error("该信息不存在！");
            }

            $this->assign("data", $data);
            return $this->fetch();
        }

    }

    /**
     * 删除广告管理
     */
    public function delete($ids = 0)
    {
        empty($ids) && $this->error('参数错误！');

        if (!is_array($ids)) {
            $ids = array($ids);
        }
      $ad_res=  $this->ads_Model->where('position_id', 'in', $ids)->find();
        if( $ad_res){
            $this->error('请先删除内容！');die;
        }
        $res = $this->AdsPosition_Model->where('position_id', 'in', $ids)->delete();
        if ($res !== false) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }

    }

    /**
     * 广告管理状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d');
        empty($id) && $this->error('参数不能为空！');
        $status = $this->request->param('status/d');
        if ($this->AdsPosition_Model->where('position_id', $id)->setField(['is_show' => $status])) {
            $this->success("操作成功！");
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 广告管理排序
     */
    public function listorder()
    {
        $id = $this->request->param('id/d', 0);
        $listorder = $this->request->param('value/d', 0);
        $rs = $this->AdsPosition_Model->where(['position_id' => $id])->setField(['listorder' => $listorder]);
        if ($rs !== false) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序失败！");
        }
    }



}
